package com.health.common.job.cycle;


import com.health.common.utils.WeBASEUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * @author zhexueqi
 * @ClassName UpdateStatusListener
 * @since 2024/7/16    11:34
 */
//@Component
@Slf4j
public class UpdateStatusListener {

//    @Resource
//    private DoctorService doctorService;

//    @Resource
    private RedisTemplate<String,Object> redisTemplate;

//    @Resource
    private RedissonClient redissonClient;

//    @Resource
//    private WeBASEUtils weBASEUtils;

//    @Value("${system.contract.owner}")
    private String owner;

    /**
     * 在每个30分钟去检查是否有挂号的时间戳超过当前时间半小时，如果超过则修改挂号状态为已过期
     */
//    @Scheduled(cron = "0 30 * * * ?")
//    public void updateStatus() {
//        //设置lock Key
//        String lockKey = String.format("%s:%s:%s:%s","health","precachejob","docache","lock");
//        // redisson获取分布式锁
//        RLock lock = redissonClient.getLock(lockKey);
//        try {
//            List<RegistrationVO> registration = new ArrayList<>();
//            if (lock.tryLock(0,10, TimeUnit.SECONDS)){
//                QueryWrapper<Doctor> queryWrapper = new QueryWrapper<>();
//                queryWrapper.select("doctorAddress");
//                List<Doctor> list = doctorService.list(queryWrapper);
//                List<String> doctorAddressList = new ArrayList<>();
//                for (Doctor doctor : list) {
//                    doctorAddressList.add(doctor.getDoctorAddress());
//                }
//                for (String doctorAddress  : doctorAddressList) {
//                    List<RegistrationVO> oneRegistration = doctorService.getOneRegistration(doctorAddress);
//                    registration.addAll(oneRegistration);
//                }
//                for (RegistrationVO registrationVO : registration) {
//                    String formattedDateTime = registrationVO.getTimestamp();
//                    // 定义解析日期时间字符串的格式
//                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//
//                    // 将字符串解析为 LocalDateTime
//                    LocalDateTime dateTime = LocalDateTime.parse(formattedDateTime, formatter);
//
//                    // 将 LocalDateTime 转换为 ZonedDateTime，使用系统默认时区
//                    ZonedDateTime zonedDateTime = dateTime.atZone(ZoneId.systemDefault());
//
//                    // 将 ZonedDateTime 转换为时间戳
//                    long timeStamp = zonedDateTime.toEpochSecond();
//                    long registrationID = registrationVO.getRegistrationID();
//                    if(timeStamp + 1800 < new Date().getTime()/1000){
//                        List<Object> funcParams = new ArrayList<>();
//                        funcParams.add(registrationID);
//                        String updateRegCancel = weBASEUtils.funcPost(owner, "updateRegCancel", funcParams);
//                        if (!JSONUtil.parseObj(updateRegCancel).get("message").toString().equalsIgnoreCase("success")){
//                            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"更新挂号状态失败");
//                        }
//                    }
//                }
//            }
//        } catch (Exception e) {
//            log.error("redis lock error", e);
//        }finally {
//            //最终都要执行，如果当前的key是当前线程，则解锁，不是则不做操作
//            if (lock.isHeldByCurrentThread()){
//                lock.unlock();
//            }
//        }
//    }

}
